package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.dao.ILinkDao;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

/**
 * 通讯录持久接口实现类
 */
@Repository
public class LinkDaoImpl extends BaseDao implements ILinkDao {
    @Override
    public CutPageBean<LinkBean> cutAll(int pageNO) {
        CutPageBean<LinkBean> cutBean = new CutPageBean<LinkBean>();
        this.setConnection();

        try {
            ps = con.prepareStatement("select * from t_link limit ?,?");
            ps.setObject(1,(pageNO - 1)*CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);

            rs = ps.executeQuery();
            while (rs.next()){
                LinkBean linkBean = this.getLink();
                cutBean.getList().add(linkBean);
            }

            ps = con.prepareStatement("select count(*) c from t_link");
            rs = ps.executeQuery();
            if(rs.next()){
                cutBean.setCount(rs.getInt("c"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }

        return cutBean;
    }

    @Override
    public void add(LinkBean link) {
        this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_link(l_name,l_birthday,l_phone) values(?,?,?)");
            ps.setObject(1,link.getName());
            ps.setObject(2,link.getBirthday());
            ps.setObject(3,link.getPhone());

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public void del(Integer id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from  t_link  where pk_linkId=?");
            ps.setObject(1,id);

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public LinkBean findById(Integer id) {
        LinkBean link = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_link where pk_linkId=?");
            ps.setObject(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                link = this.getLink();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }

        return link;
    }

    @Override
    public void update(Integer id, String phone) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update  t_link set l_phone=?  where pk_linkId=?");
            ps.setObject(1,phone);
            ps.setObject(2,id);

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    private LinkBean getLink() throws SQLException {
        LinkBean link = new LinkBean();
        link.setId(rs.getInt("pk_linkId"));
        link.setName(rs.getString("l_name"));
        String bir = rs.getString("l_birthday");
        link.setBirthday(bir != null?LocalDate.parse(bir):null);
        link.setPhone(rs.getString("l_phone"));
        return link;
    }
}
